﻿// Constants
var centralAdminPort = 26564;

//List Paths
var listPaths = [{
    "name": "Title, description and navigation", "description": "General settings for selected list", "type": "List", "url": "_layouts/ListGeneralSettings.aspx?List=", "guid": "null", "value": "_layouts/ListGeneralSettings.aspx?List=", "tokens": ["Title", "Description", "Navigation", "List"]
}, {
    "name": "Advanced settings", "description": "Advanced settings for selected list", "type": "List", "url": "_layouts/advsetng.aspx?List=", "guid": "null", "value": "_layouts/advsetng.aspx?List=", "tokens": ["List", "Advanced", "Settings", "Allow", "Managment", "Content", "Types", "Item", "Level", "Permissions", "Attachments", "Launch", "Dialog"]
}, {
    "name": "Versioning Settings", "description": "Versioning Settings", "type": "List", "url": "_layouts/15/LstSetng.aspx?List=", "guid": "null", "value": "_layouts/15/LstSetng.aspx?List=", "tokens": ["List", "Version", "Settings", "Content", "Approval", "Checkout"]
}


];

///
///Web Paths - settings paths under SPWeb 
///
var sitePaths = [{
    "name": "Site Settings", "description": "Edit current site settings.", "type": "Site", "url": "_layouts/settings.aspx", "value": "_layouts/settings.aspx", "tokens": ["_layouts/settings.aspx", "Site", "Settings"]
}, {
    "name": "People and Groups", "description": "Edit People and Groups", "type": "Site", "url": "_layouts/people.aspx", "value": "_layouts/people.aspx", "tokens": ["People", "Groups", "people.aspx", "Site"]
}, {
    "name": "Title, description and icons", "description": "Set basic site settings of current site.", "type": "Site", "url": "_layouts/prjsetng.aspx", "value": "_layouts/prjsetng.aspx", "tokens": ["Title", "Description", "Icons", "Site"]
}, {
    "name": "Content types", "description": "Manage the collection of columns available when defining lists.", "type": "Site", "url": "_layouts/mngctype.aspx", "value": "_layouts/mngctype.aspx", "tokens": ["Site", "Content", "Types", "Galleries", ]
}, {
    "name": "Term Store Management", "description": "", "type": "Site", "url": "_layouts /15/termstoremanager.aspx", "value": "_layouts /15/termstoremanager.aspx", "tokens": ["Site", "Term", "Store", "Management", ]
}, {
    "name": "Web Part Gallery", "description": "Web Designer Galleries Web Parts.", "type": "Site", "url": "_catalogs/wp", "value": "_catalogs/wp", "tokens": ["Site", "Web", "Part", "Gallery", "Galleries", ]
}, {
    "name": "Create", "description": "Select an item to create a new list, library, discussion board, survey, page or site. ", "type": "Site", "url": "_layouts/create.aspx", "value": "_layouts/create.aspx", "tokens": ["Site","Create", "Libraries", "Custom Lists", "Pages", "Sites", ]
}, {
    "name": "Site Collection Administrators", "description": "Manage Site Collection Administrators", "type": "Site", "url": "_layouts/15/mngsiteadmin.aspx", "value": "_layouts/15/mngsiteadmin.aspx", "tokens": ["Site", "Administrators", "Collection" ]
}, {
    "name": "Sites and Workspaces", "description": "Manage Sites and Workspaces", "type": "Site", "url": "_layouts/mngsubwebs.aspx", "value": "_layouts/mngsubwebs.aspx", "tokens": ["Site", "Workspaces", "Sites"]
}, {
    "name": "User Permissions", "description": "Manage User Permissions", "type": "Site", "url": "_layouts/user.aspx", "value": "_layouts/user.aspx", "tokens": ["Site", "User", "Permissions"]
}, {
    "name": "List Template Gallery", "description": "Upload, download, delete list templates", "type": "Site", "url": "_catalogs/lt", "guid": "null", "value": "_catalogs/lt", "tokens": ["Site", "List", "Template", "Gallery", "Galleries"]
}, {
    "name": "Master pages", "description": "Manage the collection of look and feel templates available to sites.", "type": "Site", "url": "_catalogs/masterpage", "value": "_catalogs/masterpage", "tokens": ["Site", "Master", "pages", "Galleries", ]
}, {
    "name": "Site Content and Structure Manager", "description": "Manage Site Content and Structure Manager", "type": "Site", "url": "_layouts/sitemanager.aspx", "value": "_layouts/sitemanager.aspx", "tokens": ["Site    ", "Content", "Structure", "Manager", ]
}, {
    "name": "Site Contents", "description": "Site Contents", "type": "Site", "url": "_layouts/viewlsts.aspx", "value": "_layouts/viewlsts.aspx", "tokens": ["Site", "Contents"]
}, {
    "name": "User Alerts", "description": "", "type": "Site", "url": "_layouts/sitesubs.aspx", "value": "_layouts/sitesubs.aspx", "tokens": ["Site", "User","Alerts"]
}, {
    "name": "Workflows", "description": "", "type": "Site", "url": "_layouts/wrkmng.aspx", "value": "_layouts/wrkmng.aspx", "tokens": ["Site", "Workflows "]
}, {
    "name": "Site Collection Features ", "description": "", "type": "Site", "url": "_layouts/ManageFeatures.aspx?Scope=Site", "value": "_layouts/ManageFeatures.aspx?Scope=Site", "tokens": ["Site", "Collection","Features","Activate","Deactivate"]
}];


///
///Web Paths - settings paths under SPWeb 
///
var webPaths = [{
    "name": "People and Groups", "description": "Edit People and Groups", "type": "Web", "url": "_layouts/people.aspx", "value": "_layouts/people.aspx", "tokens": ["Web", "People", "Groups", "people.aspx", "Users", "Permissions"]
}, {
    "name": "User permissions", "description": "Define what capabilities each user or user group can perform on this site and all subsites inheriting permissions.", "type": "Web", "url": "_layouts/user.aspx", "value": "_layouts/user.aspx", "tokens": ["Web", "user.aspx", "Users", "Permissions"]
}, {
    "name": "Site columns", "description": "Manage the collection of columns available when defining lists.", "type": "Web", "url": "_layouts/mngfield.aspx", "value": "_layouts/mngfield.aspx", "tokens": ["Web", "Columns", "Galleries", ]
}, {
    "name": "Site content types", "description": "Manage the collection of columns available when defining lists.", "type": "Web", "url": "_layouts/mngctype.aspx", "value": "_layouts/mngctype.aspx", "tokens": ["Web", "Content", "Types", "Galleries", ]
}, {
    "name": "Master pages", "description": "Manage the collection of look and feel templates available to sites.", "type": "Web", "url": "_catalogs/masterpage/", "value": "_catalogs/masterpage/", "tokens": ["Web", "Master", "pages" ]
}, {
    "name": "Regional settings", "description": "Manage the collection of look and feel templates available to sites.", "type": "Web", "url": "_layouts/regionalsetng.aspx", "value": "_layouts/regionalsetng.aspx", "tokens": ["Web", "Regional", "Settings", "Administration", ]
}, {
    "name": "Site Settings", "description": "Edit current SPWeb settings.", "type": "Web", "url": "_layouts/settings.aspx", "value": "_layouts/settings.aspx", "tokens": ["_layouts/settings.aspx", "Web", "Settings"]
}, {
    "name": "Site Content and Structure Manager", "description": "Manage Site Content and Structure Manager", "type": "Web", "url": "_layouts/sitemanager.aspx", "value": "_layouts/sitemanager.aspx", "tokens": ["Web", "Content", "Structure", "Manager", ]
}, {
    "name": "Site Contents", "description": "Site Contents", "type": "Web", "url": "_layouts/viewlsts.aspx", "value": "_layouts/viewlsts.aspx", "tokens": ["Web","Site", "Contents"]
}, {
    "name": "User Alerts", "description": "", "type": "Web", "url": "_layouts/sitesubs.aspx", "value": "_layouts/sitesubs.aspx", "tokens": ["Web", "User", "Alerts"]
}, {
    "name": "Workflows ", "description": "", "type": "Web", "url": "_layouts/wrkmng.aspx", "value": "_layouts/wrkmng.aspx", "tokens": ["Web", "Workflows "]
}, {
    "name": "Create", "description": "Select an item to create a new list, library, discussion board, survey, page or site. ", "type": "Web", "url": "_layouts/create.aspx", "value": "_layouts/create.aspx", "tokens": ["Web", "Create", "Libraries", "Custom Lists", "Pages", "Sites", ]
}, {
    "name": "Site Features ", "description": "", "type": "Web", "url": "_layouts/ManageFeatures.aspx", "value": "_layouts/ManageFeatures.aspx", "tokens": ["Web", "Features", "Activate", "Deactivate"]
}];

///
///Web Paths - settings paths under SPWeb 
///
var centralAdminPaths = [{
    "name": "Manage content databases", "description": "", "type": "Central Admin", "url": "_admin/CNTDBADM.aspx", "value": "_admin/CNTDBADM.aspx", "tokens": ["Central", "Admin", "Manage", "Content", "Databases", "CNTDBADM"]
}, {
    "name": "Manage service applications", "description": "", "type": "Central Admin", "url": "_admin/ServiceApplications.aspx", "value": "_admin/ServiceApplications.aspx", "tokens": ["Central", "Admin", "Manage", "Service", "Application", "CNTDBADM"]
}, {
    "name": "Manage services on server", "description": "", "type": "Central Admin", "url": "_admin/Server.aspx", "value": "_admin/Server.aspx", "tokens": ["Central", "Admin", "Manage", "Services", "Server"]
}, {
    "name": "Create site collections", "description": "", "type": "Central Admin", "url": "_admin/createsite.aspx", "value": "_admin/createsite.aspx", "tokens": ["Central", "Admin", "Create", "Site", "Collection"]
}, {
    "name": "View all site collections", "description": "", "type": "Central Admin", "url": "_admin/SiteCollections.aspx", "value": "_admin/SiteCollections.aspx", "tokens": ["Central", "Admin", "View", "All", "Site", "Collection"]
}, {
    "name": "Term Store Management", "description": "", "type": "Central Admin", "url": "_layouts/15/termstoremanager.aspx", "value": "_layouts/15/termstoremanager.aspx", "tokens": ["Central", "Admin", "Term", "Store", "Management"]
}
];


var searchElement = [];

searchElement.push(
    '<div class="container">',
        '<div class="place-spadminmenu-oss">',
            '<div class="wrap">',
                '<span class="spadminmenu-typeahead position">',
                    '<input class="tt-hint" type="text" autocomplete="on" spellcheck="off" disabled="" style="position: absolute; top: 0px; left: 0px; border-color: transparent; box-shadow: none; background-attachment: scroll; background-clip: border-box; background-color: rgb(255, 255, 255); background-image: none; background-origin: padding-box; background-size: auto; background-position: 0% 0%; background-repeat: repeat repeat;">',
                    '<input class="typeahead tt-query" type="text" placeholder="Search all menus" autocomplete="off" spellcheck="false" dir="auto" style="position: relative; vertical-align: top; background-color: transparent;">',
                    '<span style="position: absolute; left: -9999px; visibility: hidden; white-space: nowrap; font-family: Arial; font-size: 24px; font-style: normal; font-variant: normal; font-weight: 400; word-spacing: 0px; letter-spacing: 0px; text-indent: 0px; text-rendering: auto; text-transform: none;">',
                    '</span>',
                    '<span class="tt-dropdown-menu" style="position: absolute; top: 100%; left: 0px; z-index: 100; display: none;">',
                    '</span>',
                '</span>',
            '</div>',
        '</div>',
    '</div>');

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', PathsInit);

    $("#s4-workspace:first").append(searchElement.join(""));

    $("body").bind('click', function (e) {
        //console.log("body event");
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', PathsInit);
    });

    $(".spadminmenu-typeahead").bind('click', function (e) {
        e.stopPropagation();
    });

});//doc ready end


function CurrentState() {

    this.selectedList = function () {
        try {
            return SP.ListOperation.Selection.getSelectedList();
        } catch (e) {
            return "";
        }
    }

    this.selectedItems = function () {
        var ctx = SP.ClientContext.get_current();
        return SP.ListOperation.Selection.getSelectedItems(ctx);
    }

    this.SetListGuid = function () {
        for (var i in listPaths) {
            listPaths[i].url = listPaths[i].url.replace("LIST_GUID", this.selectedList());
            listPaths[i].value = listPaths[i].value.replace("LIST_GUID", this.selectedList());
        }
    }

    this.getPaths = function () {
        var selList = this.selectedList();
        var paths = sitePaths.concat(webPaths);
        paths = paths.concat(centralAdminPaths);
        if (typeof selList != "undefined" && selList != "" && selList != null) {
            this.SetListGuid();
            //console.log("selectedList not empty !");
            paths = paths.concat(listPaths);
            return paths;
        }
        else {
            return paths;
        }
    }


}

function PathsInit() {
    var ctx = new SP.ClientContext.get_current();
    this.web = ctx.get_web();

    ctx.load(this.web);

    ctx.executeQueryAsync(
        Function.createDelegate(this, this.GetWebRelativeUrl),
        Function.createDelegate(this, this.webRelativeUrlOnFail)
    );
}


this.GetWebRelativeUrl = function (sender, args) {
    //return this.web.get_serverRelativeUrl();
    //we got server relative url OK !
    //call function to GetPaths and Init Control

    var cs = new CurrentState();

    var serverRelativeUrl = this.web.get_serverRelativeUrl();
    if (serverRelativeUrl.length > 0) {
        serverRelativeUrl += "/";
    }

    //
    // Update paths
    //
    // Central Admin
    for (var i in centralAdminPaths) {
        centralAdminPaths[i].value = "http://" + window.location.host + ":" + centralAdminPort + "/" + centralAdminPaths[i].url;
    }

    // Site
    for (var i in sitePaths) {
        sitePaths[i].value = "http://" + window.location.host + _spPageContextInfo.siteServerRelativeUrl + "/" + sitePaths[i].url;
    }

    // Web
    for (var i in webPaths) {
        webPaths[i].value = "http://" + window.location.host + _spPageContextInfo.webServerRelativeUrl + "/" + webPaths[i].url;
    }

    // List
    if (typeof cs.selectedList() != "undefined" && cs.selectedList() != "" && cs.selectedList() != null) {
        for (var i in listPaths) {
            listPaths[i].value = "http://" + window.location.host + _spPageContextInfo.webServerRelativeUrl + "/" + listPaths[i].url + cs.selectedList();
            listPaths[i].guid = cs.selectedList();
        }
    }



    var paths = cs.getPaths();

    //console.log("rr:" + _spPageContextInfo.webServerRelativeUrl);

    //reinitialize search control with new paths ..
    $('.place-spadminmenu-oss .typeahead').typeahead('destroy');
    $('.place-spadminmenu-oss .typeahead').typeahead({
        local: paths,
        limit: 10,
        template: [
          '<p class="repo-language">{{type}}</p>',
          '<p class="repo-name">{{name}}</p>',
          '<p class="repo-description">{{description}}</p>',
          '<p class="repo-description">{{url}}</p>'
        ].join(''),
        engine: Hogan
    });

}
this.webRelativeUrlOnFail = function (sender, args) {
    //alert('Unable to get serverRelativeUrl');
}


